package controller

import (
	"github.com/gofiber/fiber"
	jsoniter "github.com/json-iterator/go"
	"gowork/nbaSchedule/model"
	"strconv"
)

type NbaScheduleController struct {
	Match *model.Match
}

func (nsc *NbaScheduleController)GenerateMovieTickets(c *fiber.Ctx){

	nsc.Match = new(model.Match)

	competitionDate := c.FormValue("competitionDate")

	competitionTime := c.FormValue("competitionTime")

	var homeTeam model.Team
	homeTeamStr := c.FormValue("homeTeam")
	uErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(homeTeamStr), &homeTeam)
	if uErr != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": uErr.Error(),
		})
		return
	}

	var visitingTeam model.Team
	visitingTeamStr := c.FormValue("visitingTeam")
	dErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(visitingTeamStr), &visitingTeam)
	if dErr != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": dErr.Error(),
		})
		return
	}

	season := c.FormValue("season")

	res , err := nsc.Match.GenerateMovieTickets(competitionDate, competitionTime, &homeTeam, &visitingTeam, season)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (nsc *NbaScheduleController)QuerySchedule(c *fiber.Ctx){

	nsc.Match = new(model.Match)

	season := c.FormValue("season")

	res , err := nsc.Match.QuerySchedule(season)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (nsc *NbaScheduleController)Ranking(c *fiber.Ctx){

	nsc.Match = new(model.Match)

	season := c.FormValue("season")

	res , err := nsc.Match.Ranking(season)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (nsc *NbaScheduleController)PlayerList(c *fiber.Ctx){

	nsc.Match = new(model.Match)

	season := c.FormValue("season")

	skillIntStr := c.FormValue("skillInt")
	skillInt, err := strconv.Atoi(skillIntStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := nsc.Match.PlayerList(season, skillInt)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (nsc *NbaScheduleController)TeamList(c *fiber.Ctx){

	nsc.Match = new(model.Match)

	season := c.FormValue("season")

	skillIntStr := c.FormValue("skillInt")
	skillInt, err := strconv.Atoi(skillIntStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := nsc.Match.TeamList(season, skillInt)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (nsc *NbaScheduleController)QueryTheBest(c *fiber.Ctx){

	nsc.Match = new(model.Match)

	matchIdStr := c.FormValue("matchId")
	matchId, err := strconv.ParseInt(matchIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := nsc.Match.QueryTheBest(matchId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

